﻿namespace ZenSystemService.Api.Utils
{
    /// <summary>
    /// 过滤查询
    /// </summary>
    public static class IQueryableExtensions
    {
        /// <summary>
        /// 过滤查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="whereString">格式见GeneratePredicates方法</param>
        /// <returns></returns>
        public static IQueryable<T> FilterByString<T>(this IQueryable<T> query, string whereString) where T : class 
        {
            if(string.IsNullOrEmpty(whereString) || query == null)
            {
                return query;
            }
            var predicates = whereString.GeneratePredicates<T>();
            if(predicates != null && predicates.Any())
            {
                foreach(var predicate in predicates)
                {
                    query = query.Where(predicate);
                }
            }
            return query;
        }
    }
}
